package p.sunmes.les.util;

import java.util.ArrayList;
import java.util.List;

public class StringUtil {

	public static int parseInt(String intString, int defaultValue) {
		int ret = defaultValue;
		try {
			ret = Integer.parseInt(intString);
		} catch (Exception e) {
		}
		return ret;
	}

	public static float parseFloat(String floatString, float defaultValue) {
		float ret = defaultValue;
		try {
			ret = Float.parseFloat(floatString);
		} catch (Exception e) {
		}
		return ret;
	}

	public static int[] parseString(String str, String delimiter) {
		String[] ss = str.split(delimiter);
		int[] ret = new int[ss.length];
		for (int i = 0; i < ss.length; i++) {
			ret[i] = parseInt(ss[i], 0);
		}
		return ret;
	}

	/**
	 * 1;2;3;4..
	 * @param str
	 * @return
	 */
	public static int[] parseString(String str) {
		String[] ss = str.split(";");
		int[] ret = new int[ss.length];
		for (int i = 0; i < ss.length; i++) {
			ret[i] = parseInt(ss[i], 0);
		}
		return ret;
	}

	/**
	 * 1_1;2_1;2_3;..
	 * @param str
	 * @return arrayList<int[]>
	 */
	public static List<int[]> parseStringArrays(String str) {
		String[] ss = str.split(";");
		List<int[]> list = new ArrayList<int[]>();
		for (String s : ss) {
			int[] arr = parseString(s, "_");
			list.add(arr);
		}
		return list;
	}

}
